package com.riaevolution.components {
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mx.containers.FormHeading;
	import mx.controls.LinkButton;


	[Event(name="iconClickEvent", type = "flash.events.Event")]

	public class FormHeadingIcon extends FormHeading {
		public static const ICONCLICK:String = "iconClickEvent";

		public function FormHeadingIcon() {
			super();
			this.setStyle('labelWidth', 1);
		}
		private var _image:LinkButton;

		override protected function createChildren():void {
			super.createChildren();
			_image = new LinkButton();
			_image.addEventListener("click", this.sendIconEvent);
			addChild(_image);
		}

		private var _iconToolTipChange:Boolean = false;
		private var _iconToolTip:String = "";

		/**
		 * Set el tool tip del iconito.
		 *
		 * @param value
		 *
		 */
		public function set iconToolTip(value:String):void {
			_iconToolTip = value;
			_iconToolTipChange = true;
			invalidateProperties();
			invalidateDisplayList();
		}

		public function get iconToolTip():String {
			return _iconToolTip;
		}

		private function sendIconEvent(event:MouseEvent):void {
			this.dispatchEvent(new Event(ICONCLICK));
		}

		override protected function measure():void {
			super.measure();
			if (measuredMinHeight < 20) {
				measuredMinHeight = 20;
			}
		}

		override protected function commitProperties():void {
			super.commitProperties();
			if (_sourceChanged) {
				_sourceChanged = false;
				_image.setStyle('icon', _source);
				_image.width = 20;
				_image.height = 20;
				invalidateDisplayList();
			}
			if (_iconToolTipChange && _image) {
				_image.toolTip = _iconToolTip;
			}
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if (_image) {
				//_image.x = unscaledWidth + Number(this.getStyle('paddingRight')) + 10;
				//_image.x = 100;
				_image.y = (unscaledHeight - _image.height) / 2 + Number(this.getStyle('paddingTop'));
			}
			if (numChildren > 1) {
				swapChildrenAt(0, 1);
			}
			if (_iconToolTipChange && _image) {
				_iconToolTipChange = false;
				_image.toolTip = _iconToolTip;
			}
		}

		private var _sourceChanged:Boolean = false;

		private var _source:Class;

		[Bindable]
		public function set icon(source:Class):void {
			_source = source;
			_sourceChanged = true;
			invalidateProperties();
		}

		public function get icon():Class {
			return _source as Class;
		}
		private var _miniMode:Boolean = false;

		public function set miniMode(value:Boolean):void {
			this._miniMode = value;
			this.updateMiniMode();
		}

		public function get miniMode():Boolean {
			return this._miniMode;
		}

		private function updateMiniMode():void {

			this._image.visible = !this._miniMode;
			this._image.includeInLayout = !this._miniMode;
			if (this._miniMode) {
				this.setStyle("indicatorGap", 0);
				this.setStyle("leading", 0);
				this.setStyle("paddingTop", 0);
			} else {
				this.setStyle("indicatorGap", 14);
				this.setStyle("leading", 2);
			}
		}
	}
}